/** 
 * Project Name:jd-orm 
 * File Name:CRUDConfiguration.java 
 * Package Name:com.jd.orm.framework.configuration 
 * Date:2014-5-12下午7:39:56 
 * Copyright (c) 2014, liubing1@jd.com All Rights Reserved. 
 * 
 */

package com.jd.orm.framework.configuration;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.jd.orm.framework.configuration.bean.CRUDConfigurationBean;
import com.jd.orm.framework.configuration.bean.DeleteConfigurationBean;
import com.jd.orm.framework.configuration.bean.InsertConfigurationBean;
import com.jd.orm.framework.configuration.bean.QueryConfigurationBean;
import com.jd.orm.framework.configuration.bean.UpdateConfigurationBean;
import com.jd.orm.framework.core.exception.DbError;
import com.jd.orm.framework.core.exception.SysError;
import com.jd.orm.framework.core.exception.SystemException;
import com.thoughtworks.xstream.XStream;

/**
 * ClassName: CRUDConfiguration <br/>
 * Function: CRUD configuration property. <br/>
 * Reason: TODO ADD REASON. <br/>
 * date: 2014-5-12 下午7:39:56 <br/>
 * 
 * @author liubing1@jd.com
 * @version
 * @since up JDK 1.5
 */

public class CRUDConfiguration {

	public static CRUDConfigurationBean getCRUDConfigurationBean(String xml) {
		try {
			String content = readFile(xml);
			XStream stream = new XStream();

			stream.alias("CRUD", CRUDConfigurationBean.class);

			stream.aliasField("querys", CRUDConfigurationBean.class,
					"queryBeanConfigurations");
			stream.aliasField("inserts", CRUDConfigurationBean.class,
					"insertBeanConfigurations");
			stream.aliasField("updates", CRUDConfigurationBean.class,
					"updateBeanConfigurations");
			stream.aliasField("deletes", CRUDConfigurationBean.class,
					"deleteConfigurations");

			stream.alias("query", QueryConfigurationBean.class);
			stream.alias("insert", InsertConfigurationBean.class);
			stream.alias("update", UpdateConfigurationBean.class);
			stream.alias("delete", DeleteConfigurationBean.class);

			stream.useAttributeFor(CRUDConfigurationBean.class, "name");
			stream.useAttributeFor(CRUDConfigurationBean.class, "className");

			stream.useAttributeFor(QueryConfigurationBean.class, "returnClass");
			stream.useAttributeFor(QueryConfigurationBean.class,
					"parameterStyle");
			stream.useAttributeFor(QueryConfigurationBean.class,
					"returnEntityClass");
			stream.useAttributeFor(QueryConfigurationBean.class, "id");

			stream.useAttributeFor(InsertConfigurationBean.class,
					"parameterStyle");
			stream.useAttributeFor(InsertConfigurationBean.class, "id");

			stream.useAttributeFor(UpdateConfigurationBean.class,
					"parameterStyle");
			stream.useAttributeFor(UpdateConfigurationBean.class, "id");

			stream.useAttributeFor(DeleteConfigurationBean.class,
					"parameterStyle");
			stream.useAttributeFor(DeleteConfigurationBean.class, "id");

			CRUDConfigurationBean crudConfigurationBean = (CRUDConfigurationBean) stream
					.fromXML(content);
			return crudConfigurationBean;
		} catch (Exception e) {
			// TODO: handle exception
			throw SystemException.unchecked(e, DbError.IO_ERROR);
		}
	}

	/**
	 * 
	 * readFile: <br/>
	 * get content from file<br/>
	 * 
	 * @author liubing1@jd.com
	 * @param fileName
	 * @return
	 * @throws Exception
	 * @since JDK 1.5
	 */
	public static String readFile(String fileName) {
		try {
			String fileContent = "";
			File f = new File(fileName);
			FileReader fileReader = new FileReader(f);
			BufferedReader reader = new BufferedReader(fileReader);
			String line = "";
			while ((line = reader.readLine()) != null) {
				fileContent = fileContent + line;
			}
			reader.close();
			return fileContent;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			throw SystemException.unchecked(e, SysError.FILE_NOT_FOUND);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw SystemException.unchecked(e, DbError.IO_ERROR);
		}
	}
}
